package com.redisdemo.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * <h3>redisDemo</h3>
 * <p>复制文件追加模式</p>
 *
 * @author gdw
 * @since 2024-07-02 16:05:13
 */
public class FileCopyZjTask implements Runnable {
    private final String sourceFilePath;
    private final String destFilePath;
    private final long start;
    private final long size;

    public FileCopyZjTask(String sourceFilePath, String destFilePath, long start, long size) {
        this.sourceFilePath = sourceFilePath;
        this.destFilePath = destFilePath;
        this.start = start;
        this.size = size;
    }
    @Override
    public void run() {
        try (RandomAccessFile sourceFile = new RandomAccessFile(sourceFilePath, "r");
             RandomAccessFile destFile = new RandomAccessFile(destFilePath, "rw")) {

            byte[] buffer = new byte[(int) size];
            sourceFile.seek(start); // 定位到开始位置
            sourceFile.readFully(buffer); // 读取数据到缓冲区

            destFile.seek(destFile.length()); // 定位到目标文件末尾
            destFile.write(buffer); // 写入数据到目标文件末尾

            System.out.println(Thread.currentThread().getName() + ": Appended " + size + " bytes from position " + start);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
